package fr.utbm.aiechat;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

import fr.utbm.aiechat.structures.SharedFile;
import fr.utbm.aiechat.structures.SharedFileAdapter;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.Toast;

public class SharedFilesManager extends AsyncTask<Void, Object, Void>
{
	private static SharedFilesManager instance = null;
	
	private Activity myActivity;
	
	private ArrayList<SharedFile> myFiles;
	
	public SharedFileAdapter userFiles;
	
	private ServerSocket SocketServer;
	
	public ArrayList<SharedFile> downloadFiles;
	
	private boolean started;
	
	
	public SharedFilesManager(Activity myActivity)
	{
		this.myActivity = myActivity;
		
		started = false;
		
		userFiles = new SharedFileAdapter(myActivity, R.layout.sharedfiles_list);
		
		myFiles = new ArrayList<SharedFile>();
		
		downloadFiles = new ArrayList<SharedFile>();
		
		File sdcard = Environment.getExternalStorageDirectory();
		
		if(sdcard.isDirectory())
		{
			String files[] = sdcard.list();
			
			for(int i=0; i < files.length; i++)
			{
				File file = new File(sdcard, files[i]);
				
				if(file.isDirectory())
					continue;
				
				if(files[i].startsWith("."))
					continue;
				
				try
				{
					myFiles.add(new SharedFile(files[i], file.length(), Network.getInstance().getMyAddress()));
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}

	// Singleton Pattern
	public static SharedFilesManager getInstance()
	{
		return instance;
	}
	
	public boolean started()
	{
		return started;
	}
	
	// Singleton Pattern
	public static SharedFilesManager createInstance(Activity MyActivity)
	{
		if(instance == null)
		{
			instance = new SharedFilesManager(MyActivity);
		}
		return instance;
	}
	
	public ArrayList<SharedFile> getMyFiles()
	{
		return myFiles;
	}
	
	public void startDownload(SharedFile file) throws IOException
	{
		if(file.getOwner().equals(Network.getInstance().getMyAddress()))
		{
			Toast.makeText(myActivity, "You can't download your own Files idiot !", Toast.LENGTH_SHORT).show();
			return;
		}
		
		downloadFiles.add(file);
		
		Network.getInstance().askForFile(file);
	}

	
	public long getDownloadedFileSize(String filename)
	{
		Iterator<SharedFile> it = downloadFiles.iterator();
		
		while(it.hasNext())
		{
			SharedFile file = it.next();
			
			if(file.getName().compareTo(filename) == 0)
			{
				return file.getSize();
			}
		}
		return -1;
	}
	
	
	@Override
	protected Void doInBackground(Void... params)
	{
		started = true;
		
		try
		{
			SocketServer = new ServerSocket(6668);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		while(true)
		{
			try {
				Socket client = SocketServer.accept();
				
				FileReceiver fr = new FileReceiver(client, myActivity);
				
				fr.execute();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
